package com.library.service.implementation.account;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.library.entities.Account;
import com.library.entities.Role;

public class SecurityAccount extends Account implements UserDetails{

	private static final long serialVersionUID = 1L;
	
	public SecurityAccount(Account account) {
		if (account != null) {
			this.setId(account.getId());
			this.setUsername(account.getUsername());
			this.setPassword(account.getPassword());
			this.setRoles(account.getRoles());
		}
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> securityAuthorities = new ArrayList<>();
		List<Role> userRoles = this.getRoles();
		if (userRoles != null) {
			for (Role role : userRoles) {
				SimpleGrantedAuthority securityAuthority = new SimpleGrantedAuthority(
						role.getName());
				securityAuthorities.add(securityAuthority);
			}
		}
		return securityAuthorities;
	}
	
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
